package com.sinog.biz.util;

import com.sinog.biz.util.smlib.Sm4;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;

/**
 * @desc 整合系统中使用的各种main函数
 * @author lzq
 * @date 2021-04-29 16:00
 */
@Slf4j
public class MainUtil {

    public static void main(String[] args) throws Exception {
        main666();
    }

    //不会报空指针
    public static void main666() {
        String mainA = null;
        if (StringUtils.isEmpty(mainA)) {
            System.out.println("womn");
        }
    }

    public static void main33() {
        String strid = IdGen.strid();
        System.out.println(strid);
    }

    /**
     * 生成用户密码:oauth2用户名/加密密码
     */
    private static void main1() {
        String pass = "1";
        BCryptPasswordEncoder bpe = new BCryptPasswordEncoder();
        long start = System.currentTimeMillis();
        String hashPass = bpe.encode(pass);
        log.error(hashPass);
        boolean f = bpe.matches("asdf@123",hashPass);
        long end = System.currentTimeMillis();
        log.error("耗时：{}",end - start);
        log.error("是否匹配：{}",f);
    }

    /**
     * 身份证加密-sm4
     */
    public static String main2(String json) {
        try {
            String value = Sm4.encryptEcb(json);
            log.info("加密前源数据---" + json);
            log.info("加密后数据---" + value);
            return value;
        } catch(RuntimeException e) {
            log.error("发生异常:",e);
        }
        return null;
    }

    /**
     * 身份证解密-sm4
     */
    public static String main21(String json) {
        String value = null;
        try {
            value = Sm4.decryptEcb(json);
            log.info("解密前源数据---" + json);
            log.info("解密后数据---" + value);
        } catch(RuntimeException e) {
            log.error("发生异常:",e);
        }
        return value;
    }

    /**
     * 数据库密码加密-DES加密
     */
    public static String main3(String content) {
        //待加密的密码明文
        log.info("加密前字符串:{},加密前16进制:{} ",content,DesCryptoGraphy.byteToHexString(content.getBytes()));
        byte[] encrypted = DesCryptoGraphy.desCbcEncrypt(content.getBytes(),DesCryptoGraphy.KEY.getBytes());
        assert null != encrypted;
        log.info("加密后16进制:{}",DesCryptoGraphy.byteToHexString(encrypted));
        byte[] decrypted = DesCryptoGraphy.desCbcDecrypt(encrypted,DesCryptoGraphy.KEY.getBytes());
        assert null != decrypted;
        log.info("解密后字符串:{},解密后16进制:{}",new String(decrypted),DesCryptoGraphy.byteToHexString(decrypted));
        return DesCryptoGraphy.byteToHexString(encrypted);
    }

    /**
     * 数据库密码解密-DES加密
     */
    public static String main31(String content) {
        //待加密的密码明文
        byte[] encrypted = DesCryptoGraphy.desCbcDecrypt(DesCryptoGraphy.hexStringToByte(content),DesCryptoGraphy.KEY.getBytes());
        assert null != encrypted;
        return new String(encrypted);
    }

    /**
     * 2.4.1上线书写,更改数据库中标字段的顺序
     */
    private static void main4() throws SQLException {
        String driver = "com.mysql.cj.jdbc.Driver";
        String url = "jdbc:mysql://39.104.161.9:3307/azbj_henan0914_dev?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=UTF-8&allowMultiQueries=true&zeroDateTimeBehavior=CONVERT_TO_NULL";
        String userName = "root";
        String password = "Gkzx!2020";
        String dataBase1 = "azbj_henan0914_dev";
        JdbcUtils.connectSql(driver,url,userName,password);
        JdbcUtils.executeSql2(dataBase1);
    }

    /**
     * 获得当前时间戳
     */
    public static void main5() {
        log.info(String.valueOf(System.currentTimeMillis()));
    }

    /**
     * 判断是否是数字字符串
     * @param str 判断字符串
     * @return true/false
     */
    public static boolean isNumber(String str) {
        Pattern pattern = Pattern.compile("[0-9]*");
        return pattern.matcher(str).matches();
    }

    /**
     * 安置帮教最新的社矫账号使用司法所的部门编号+X来标识,由于天津和四川的账号创建过早,社矫的部门是随机标识的,为了与安置帮教所有社矫统一,目前编写社矫账号迁移功能来做对应处理
     * @param fileUrl 整理的文件路径
     */
    public static void main6(String fileUrl) {
        try {
            // 解析路径的file文件
            Workbook workbook = Workbook.getWorkbook(new File(fileUrl));
            // 获取第一张工作表
            Sheet sheet = workbook.getSheet(0);
            // 循环获取每一行数据 因为默认第一行为标题行，我们可以从 1 开始循环，如果需要读取标题行，从 0 开始
            // sheet.getRows() 获取总行数
            for (int i = 1; i < sheet.getRows(); i++) {
                // 获取第一列的第 i 行信息 sheet.getCell(列，行)，下标从0开始
                String oldCodeId = sheet.getCell(0, i).getContents();
                // 获取第二列的第 i 行信息
                String newCodeId = sheet.getCell(1,i).getContents();
                log.info("update tbsys_department set id = '{}X' where id = '{}';",newCodeId,oldCodeId);
                log.info("update prisoner_main set adduserid =  '{}X' where adduserid = '{}';",newCodeId,oldCodeId);
                log.info("update prisoner_main set pdanwei = '{}X' where pdanwei = '{}';",newCodeId,oldCodeId);
                log.info("update prisoner_fx set adduserid = '{}X' where adduserid = '{}';",newCodeId,oldCodeId);
                log.info("update prisoner_sr set adduserid = '{}X' where adduserid = '{}';",newCodeId,oldCodeId);
                log.info("update prisoner_zn set adduserid = '{}X' where adduserid = '{}';",newCodeId,oldCodeId);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException ee) {
            ee.printStackTrace();
        }
    }

    public static void main7() {
        List<String> list = new ArrayList<>();
        list.add("1111222");
        list.add("222222");
        list.add("333");
        list.add("444");
        list.add("555");
        list.add("6666");
        System.out.println(list.subList(2,5));
    }

    public static void main9() {
        String url = "delete from prisave_temp where pri_num='4124001220000008'";

    }
}